# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=f2c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0
pkgrel=1
pkgdesc='Fortran to C code translator (mingw-w64)'
url='https://www.netlib.org/f2c/index.html'
license=('custom')
makedepends=('unzip')
options=('strip' 'staticlibs')
arch=('any')
options=('!buildflags' 'staticlibs')
source=("https://www.netlib.org/f2c/libf2c.zip"
        20051004-add-ofiles-dep.patch
        20090407-cygwin-build.patch
        libf2c-20110801-64bit-long.patch
        libf2c-1.patch)
noextract=("libf2c.zip")
sha256sums=('ca404070e9ce0a9aaa6a71fc7d5489d014ade952c5d6de7efb88de8e24f2e8e0'
            '3f688297b45515c1e386bb7f94ad36bf0f6a3497a0baff759b54d943cbec7d8f'
            '7fdf61868985bd116307e0039967666845c74c09dc3a3293974ff34d374ac9c6'
            '919698643f12b2fc4b9f4ebac395a8b2c9c01e83028907621b95dee8db4d5653'
            '0fc51325773b0e74c89aafd3b0331b76ad9890b060ac2d0fa2fc6aa72b93a249')

prepare() {
  cd "${srcdir}"

  # zipbomb
  rm -rf libf2c 
  mkdir -p libf2c && pushd libf2c
  unzip "${srcdir}"/libf2c.zip

  # mxe patch
  patch -p1 -i "${srcdir}"/20051004-add-ofiles-dep.patch
  patch -p1 -i "${srcdir}"/20090407-cygwin-build.patch
  patch -p1 -i "${srcdir}"/libf2c-20110801-64bit-long.patch
  patch -p1 -i "${srcdir}"/libf2c-1.patch
  popd
}

build() {
  cd "${srcdir}"
  cp -r libf2c build-${CARCH} && pushd build-${CARCH} 
  local _cc="CC=${MINGW_PREFIX}/bin/gcc"
  local _ar="AR=${MINGW_PREFIX}/bin/ar"
  local _ld="LD=${MINGW_PREFIX}/bin/ld"
  local _ranlib="RANLIB=${MINGW_PREFIX}/bin/ranlib"
  make -f makefile.u ${_ar} ${_cc} ${_ld} ${_ranlib} \
   CFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4 -DUSE_CLOCK'
  popd
}

package() {
    cd "$srcdir/build-${CARCH}"
    install -Dm644 f2c.h "${pkgdir}"${MINGW_PREFIX}/include/f2c.h
    install -Dm644 libf2c.a "${pkgdir}"${MINGW_PREFIX}/libf2c.a
#    #${_arch}-strip --strip-unneeded "$pkgdir"${MINGW_PREFIX}/bin/*.dll
#    ${_arch}-strip -g "$pkgdir"${MINGW_PREFIX}/lib/*.a
} 

